//
//  ShareView.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/7/30.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "ShareView.h"
#import <TencentOpenAPI/QQApiInterface.h>
#import "WXApi.h"
#import "WeiboSDK.h"

@implementation ShareView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = REXADECIMALCOLOR(@"#f4f4f4");
        
        NSMutableArray *shareArr = [NSMutableArray array];
//        if ([WXApi isWXAppInstalled]) {
            [shareArr addObject:@"微信好友"];
            [shareArr addObject:@"朋友圈"];
//        }
//        if ([WeiboSDK isWeiboAppInstalled]) {
            [shareArr addObject:@"新浪微博"];
//        }
//        if ([QQApiInterface isQQInstalled]) {
            [shareArr addObject:@"QQ好友"];
            [shareArr addObject:@"QQ空间"];
//        }
        
        CGFloat margin = (SCREEN_WIDTH - ScreenX375(55) * 5) / 6;
        for (int i = 0; i < shareArr.count; i++) {
            UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(margin + i * (ScreenX375(55) + margin), ScreenX375(87), ScreenX375(55), ScreenX375(17))];
            nameLabel.text = shareArr[i];
            nameLabel.font = Kfont(12);
            nameLabel.textAlignment = NSTextAlignmentCenter;
            nameLabel.textColor = REXADECIMALCOLOR(@"#2f2933");
            nameLabel.tag = 200 + i;
            [self addSubview:nameLabel];
            
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(margin + i * (ScreenX375(55) + margin), ScreenX375(26), ScreenX375(55), ScreenX375(55))];
            [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"分享-%@",shareArr[i]]] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = 100 + i;
            [self addSubview:button];
        }
        
        UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, ScreenX375(130), SCREEN_WIDTH, ScreenX375(50))];
        cancelBtn.backgroundColor = [UIColor whiteColor];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:REXADECIMALCOLOR(@"#2f2933") forState:UIControlStateNormal];
        cancelBtn.titleLabel.font = Kfont(16);
        [cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:cancelBtn];
    }
    return self;
}

- (void)buttonClick:(UIButton *)button {
    UILabel *nameLabel = [self viewWithTag:button.tag + 100];
    if ([nameLabel.text isEqualToString:@"微信好友"]) {
        if (_wxClick) {
            _wxClick();
        }
    }else if ([nameLabel.text isEqualToString:@"朋友圈"]) {
        if (_wxGroupClick) {
            _wxGroupClick();
        }
    }else if ([nameLabel.text isEqualToString:@"QQ好友"]) {
        if (_qqClick) {
            _qqClick();
        }
    }else if ([nameLabel.text isEqualToString:@"QQ空间"]) {
        if (_qqGroupClick) {
            _qqGroupClick();
        }
    }else if ([nameLabel.text isEqualToString:@"新浪微博"]) {
        if (_wbClick) {
            _wbClick();
        }
    }
}

- (void)cancelAction {
    if (_cancel) {
        _cancel();
    }
}

@end
